﻿using System;
using System.Windows.Forms;
using SSAT.Utils;

namespace SSAT.Controls
{
    public partial class LoadingProgress : UserControl
    {
        private const string LoadingProgressIndicatorChar = ".";
        private const int MaxIndicatorCharacters = 4;

        private int _сounter = 0;
        private string[] _loadingTexts;

        public LoadingProgress()
        {
            InitializeComponent();
            CreateLoadingIndicatorTexts();
        }

        private void CreateLoadingIndicatorTexts()
        {
            _loadingTexts = new string[MaxIndicatorCharacters];
            for (int i = 0; i < _loadingTexts.Length; i++)
            {
                string indicatorChars = string.Empty;
                for (int j = 0; j < i; j++)
                {
                    indicatorChars += LoadingProgressIndicatorChar;
                }
                _loadingTexts[i] = Resources.Loading + indicatorChars;
            }
        }

        private void tmrLoading_Tick(object sender, EventArgs e)
        {
            _сounter++;
            lblLoading.Text = _loadingTexts[_сounter % MaxIndicatorCharacters];
        }

        public void Start()
        {
            UIHelper.InvokeSafely(this, Enable);
        }

        private void Enable()
        {
            tmrLoading.Enabled = true;
            Visible = true;
        }

        public void Finish()
        {
            UIHelper.InvokeSafely(this, Disable);
        }

        private void Disable()
        {
            tmrLoading.Enabled = false;
            _сounter = 0;
            Visible = false;
        }
    }
}
